package com.webserver.core;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.controller.UserController;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import org.apache.log4j.Logger;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 保存所有请求与对应的业务处理关系
 * @Author Alex
 */
public class HandlerMapping {
    private static Logger log = Logger.getLogger(UserController.class);

    /*
        key:请求路径
        value:处理该请求的业务类及对应方法

        例如:
        key:/myweb/reg
        value:MethodMapping{
            controller:UserController
            method:reg()
        }
     */
    private static Map<String,MethodMapping> mapping = new HashMap<>();

    static{
        initMapping();
    }

    private static void initMapping() {
        /*
            扫描com.webserver.controller包中的所有类,并将所有被注解@Controller
            的类实例化,然后将@RequestMapping标注的方法获取,并将这两项以一个
            MethodMapping表示.最终将@RequestMapping注解传递的参数(请求路径)作为
            key,将这个MethodMapping作为value保存到mapping这个map
         */
        try {
            File dir = new File(
                    HandlerMapping.class.getClassLoader().getResource("./com/webserver/controller").toURI()
            );
            System.out.println("相对路径:"+dir.getName());//controller
            File[] subs = dir.listFiles(f->f.getName().endsWith(".class")) ;
            for (File sub:subs){
                String fileName = sub.getName();
                String className = fileName.substring(0,fileName.lastIndexOf("."));
                Class cls = Class.forName("com.webserver.controller."+className);
                //判断该类是否被注解@Controller标注
                if(cls.isAnnotationPresent(Controller.class)){
                    //实例化对象
                    Object controller = cls.newInstance();
                    //获取类中的方法
                    Method[] methods = cls.getDeclaredMethods();
                    for (Method method:methods){
                        //判断该方法是否被RequestMapping标注
                        if (method.isAnnotationPresent(RequestMapping.class)){
                            RequestMapping rm = method.getAnnotation(RequestMapping.class);
                            String value = rm.value();
                            MethodMapping mm = new MethodMapping(controller,method);
                            mapping.put(value,mm);
                        }
                    }
                }
            }
        } catch (URISyntaxException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 根据请求路径获取对应的业务处理类
     * @param path
     * @return
     */
    public static MethodMapping getMethodMapping(String path){
        return mapping.get(path);
    }

    /**
     * 内部类
     */
    public static class MethodMapping{
        //某个Controller实例
        private Object controller;
        //对应的业务处理方法
        private Method method;

        public MethodMapping(Object controller, Method method) {
            this.controller = controller;
            this.method = method;
        }

        public Object getController() {
            return controller;
        }

        public void setController(Object controller) {
            this.controller = controller;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }
    }
}
